package com.wdcg.fragment;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.wdcg.R;
import com.wdcg.activity.MainActivity;
import com.wdcg.model.access.BonusAccess;
import com.wdcg.model.dao.BonusDescription;
import com.wdcg.view.adapter.BonusesListAdapter;

/**
 * @author Grzegorz Matyszczak<BR>
 *
 */
public class BonusesFragment extends Fragment implements OnItemClickListener {
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.fragment_bonuses, container, false);
		return v;
	}
	
	@Override
	public void onResume() {
		super.onResume();		
        getActivity().getActionBar().setTitle(R.string.action_bonuses_title);

		ListView bonusesListView = (ListView) getActivity().findViewById(R.id.bonusesListView);
		bonusesListView.setAdapter(new BonusesListAdapter(getActivity(), BonusAccess.getActiveBonusesDescription()));
		
		bonusesListView.setOnItemClickListener(this);
		
	}

	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		BonusDescription bonus = (BonusDescription) parent.getItemAtPosition(position);
		
		BonusInfoFragment bonusInfoFragment = new BonusInfoFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(getString(R.string.bundle_bonus_descr_id), bonus.getId());
		bonusInfoFragment.setArguments(bundle);

		((MainActivity)getActivity()).setPreviousFragment(this);
		
		FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
		fragmentTransaction.replace(R.id.fragmentContainer, bonusInfoFragment);
		fragmentTransaction.commit();
		ActionBar actionBar = getActivity().getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);        
	}
	
}
